<template>
  <div class="role">
    <div class="">
      <el-form :inline="true">
        <el-form-item label="">
          <el-input v-model="role_name" placeholder="名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">查询</el-button>
          <el-button @click="add" v-permission="['管理者']">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="tableData"
      border
    >
      <el-table-column
        prop="id"
        label="序号"
      />
      <el-table-column
        prop="role_name"
        label="角色"
      />
      <el-table-column
        prop="describe_str"
        label="描述"
      />
      <el-table-column
        label="操作"
      >
        <template slot-scope="scope">
          <el-link size="mini" type="primary" @click="getDetails(scope.row)" v-permission="['管理者']" style="margin-right: 10px">编辑</el-link>
          <el-link v-if="scope.row.id !== 1" size="mini" type="primary" @click="handleDelete(scope.row)" v-permission="['管理者']">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="currentPage"
      @size-change="handleSizeChange"
      @current-change="handleSizeChange"
      :page-size="10"
      layout="total, prev, pager, next, jumper"
      :total="total"
    />

    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-input v-model="ruleForm.role_name" placeholder="角色名称" />
      <el-input
        v-model="ruleForm.desc"
        style="margin: 20px 0"
        type="textarea"
        resize="none"
        rows="4"
        placeholder="描述"
      />
      <el-tree
        ref="tree"
        :data="options"
        show-checkbox
        node-key="menu_id"
        :props="defaultProps"
      />
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { roleList, createOrUpdateRole, roleMenus, getRoleDetails, deleteRole } from '@/api/system'
export default {
  data() {
    return {
      role_name: '',
      tableData: [],
      currentPage: 1,
      total: 0,
      dialogVisible: false,
      ruleForm: {
        id: null,
        role_name: "",
        desc: "",
        menu_ids: [],
        type: ""
      },
      title: '新增',

      options: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  methods: {
    add() {
      this.dialogVisible = true
      this.title = '新增'
      this.ruleForm = {
        id: null,
        role_name: "",
        desc: "",
        menu_ids: [],
        type: ""
      }
    },
    async handleDelete(item) {
      const res = await deleteRole(item.id)
      if (res.code == 200) {
        this.$message.success('删除成功')
        this.getList()
      }
    },
    async getDetails(item) {
      const params = {
        page_num: this.currentPage,
        account: this.account,
        page_size: 10,
        id: item.id,
        conditions: [
          {
            key: "role_name",
            operator: "like",
            value: item.role_name
          }
        ],
        order_info: {
          key: "create_time",
          order: "desc",
        },
      }
      const res = await getRoleDetails(params)
      if (res.code == 200) {
        this.title = '编辑角色'
        this.dialogVisible = true
        this.ruleForm = {
          id: item.id,
          role_name: res.data.role_name,
          desc: res.data.desc,
          menu_ids: res.data.role_menu_ids,
          type: ""
        }
        this.$nextTick(()=>{
          this.$refs.tree.setCheckedKeys(res.data.role_menu_ids)
        })
        console.log(this.ruleForm)
      }
    },
    async addConfirm() {
      const params = {
        id: this.ruleForm.id,
        role_name: this.ruleForm.role_name,
        desc: this.ruleForm.desc,
        menu_ids: this.$refs.tree.getCheckedKeys(),
        type: ""
      }
      const res = await createOrUpdateRole(params)
      if (res.code == 200) {
        this.dialogVisible = false
        this.$message.success('保存成功')
        this.getList()
      }
    },
    async getRoleMenus () {
      const res = await roleMenus()
      if (res.code == 200 && res.data) { 
        this.options = res.data
      }
    },
    async getList() {
      const params = {
        page_num: this.currentPage,
        account: this.account,
        page_size: 10,
        conditions: [
          {
            key: "role_name",
            operator: "like",
            value: this.role_name
          }
        ],
        order_info: {
          key: "create_time",
          order: "desc",
        },
      }
      const res = await roleList(params);
      if (res.code == 200 && res.data) {
        this.tableData = res.data
        this.total = res.paging.total
      }
    },
    handleSizeChange(val) {
      this.currentPage = val
      this.getList()
    },
    handleClose(done) {
      done()
    },
    seeDetail(id) {
      this.$router.push('/question/detail')
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    }
  },
  mounted() {
    this.getList()
    this.getRoleMenus()
  }
}
</script>

<style lang="scss" scoped>
.role{
  position: relative;
  min-height: calc(100vh - 150px);
  padding-bottom: 100px;
  .el-pagination{
    position: absolute;
    bottom: 0;
    left: 50%;
    margin-left: - 300px;
    width: 600px;
  }
}
</style>
